define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/privatesea/index' + location.search,
                    add_url: 'business/privatesea/add',
                    edit_url: 'business/privatesea/edit',
                    del_url: 'business/privatesea/del',
                    multi_url: 'business/privatesea/multi',
                    import_url: 'business/privatesea/import',
                    recovery_url: 'business/privatesea/recovery',
                    randadd_url: 'business/privatesea/randadd',
                    info_url: 'business/info/index',
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'gender', title: __('Gender'), searchList: {"0":__('Secrecy'),"1":__('Male'),"2":__('FeMale')}, formatter: Table.api.formatter.normal},
                        {field: 'source.name', title: __('Source'),searchList:Config.sourceList},
                        {field: 'deal', title: __('Deal'), searchList: {"0":__('Deal 0'),"1":__('Deal 1')}, formatter: Table.api.formatter.normal},
                        {field: 'openid', title: __('Openid'), operate: 'LIKE'},
                        {field: 'province_text', title: __('Province'), operate: 'LIKE'},
                        {field: 'city_text', title: __('City'), operate: 'LIKE'},
                        {field: 'district_text', title: __('District'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'email', title: __('Email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'auth', title: __('Auth'), searchList: {"0":__('Auth 0'),"1":__('Auth 1')}, formatter: Table.api.formatter.normal},
                        {field: 'poster', title: __('Poster'), operate: 'LIKE', formatter: Table.api.formatter.image,events: Table.api.events.image,},
                        {field: 'parentid', title: __('Parentid')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                            {
                                name: 'recovery', 
                                icon: 'fa fa-recycle', 
                                title: '回收',
                                confirm: '确定要回收吗', 
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-success btn-ajax',
                                url: $.fn.bootstrapTable.defaults.extend.recovery_url,
                                success: () => $(".btn-refresh").trigger("click")
                            },
                            {
                                name: 'info',
                                title: '客户详情',
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                                classname: "btn btn-xs btn-success btn-dialog",
                                icon: 'fa fa-eye',
                                url: $.fn.bootstrapTable.defaults.extend.info_url,
                            }
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $('.btn-randadd').click(function(){
                Fast.api.open($.fn.bootstrapTable.defaults.extend.randadd_url, '随机添加',{'area':['260px','260px']})
            })
            $('.btn-recovery').click(function(){
                var ids = Table.api.selectedids(table);
                
                layer.confirm('确定要回收吗?', { title: '回收', btn: ['是', '否'] },function (index) {
                    $.post($.fn.bootstrapTable.defaults.extend.recovery_url, {ids: ids}, function (response) {
                        layer.close(index)

                        if(response.code) 
                        {
                            Toastr.success(response.msg)
                            $(".btn-refresh").trigger('click');
                        }else
                        {
                            Toastr.error(response.msg)
                        }
                    }, 'json')
                })
            })
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'business/privatesea/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'business/privatesea/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'business/privatesea/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.getRegion();
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.getRegion();
            Controller.api.bindevent();
        },
        randadd: function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            getRegion: function(){
                $("#c-region").on("cp:updated", function() {
                    var citypicker = $(this).data("citypicker");
                    var code = citypicker.getCode("province")+','+citypicker.getCode("city")+','+citypicker.getCode("district");
                    $("#c-code").val(code);
                  });
            }
        }
    };
    return Controller;
});
